package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingcartMapper;
import com.sky.service.ShoppingcartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ShoppingcartServiceimpl implements ShoppingcartService {
    @Autowired
    private ShoppingcartMapper shoppingcartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addshoppingcart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userid=BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);
       List<ShoppingCart>list= shoppingcartMapper.list(shoppingCart);
       //查到直接进行数量加一
        if(list!=null&&list.size()>0)
        {
            ShoppingCart cart=list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingcartMapper.update(cart);

        }
        else {//购物车没有数据判断是菜品还是套餐
            Long dishid=shoppingCartDTO.getDishId();
            Long setmealid=shoppingCartDTO.getSetmealId();
            if(dishid!=null){


                Dish dish=dishMapper.selectbyid(dishid);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }
            else{
                Setmeal setmeal=setmealMapper.getById(setmealid);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingcartMapper.addshoppingcart(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> listall() {
        return shoppingcartMapper.listall();
    }

    @Override
    public void deleteone(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userid=BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);
        List<ShoppingCart>list= shoppingcartMapper.list(shoppingCart);
        ShoppingCart cart=list.get(0);
        //查到数量大于等于2时直接减一，等于一时才进行消除
        if(cart.getNumber()>1)
        {

            cart.setNumber(cart.getNumber()-1);
            shoppingcartMapper.update(cart);

        }

else {
        shoppingcartMapper.deleteone(shoppingCart);
}
    }

    @Override
    public void deleteall() {
        Long userid=BaseContext.getCurrentId();
        shoppingcartMapper.deleteall(userid);
    }

}
